import struct,thread,time
from socket import *

header  = 'HTCP'
server  = 0x06
action  = 0x01
alen    = 17
ver     = 0
data    = '1111122222333331'
crc     = 0

result = getaddrinfo("lock.htcharge.com", None)
#result = getaddrinfo("127.0.0.1", None)
ip,ssstatus = result[0][4]
print ip
s=socket(AF_INET,SOCK_STREAM,0)
s.connect((ip,9001))

def sendData(a):
    count = 1
    try:
        while 1:
            print
            print "****************************************************************"
            if (count == 0) :
                count = 1
                senddata = struct.pack("!4sBBBB16sH", header, server, 1, alen, ver, data, crc)
            else:
                count = 0
                senddata = struct.pack("!4sBBBB16sH", header, server, 2, alen, ver, data, crc)
            s.send(senddata)
            print "send:"
            for c in senddata:
                print hex(ord(c)),
            print
            time.sleep(15)
    except Exception,ex:
        print ex

def recvData(b):
    try:
        while 1:
            res=s.recv(1024)
            print "recv:"
            for c in res:
                print hex(ord(c)),
            print
    except Exception,ex:
        print ex

if __name__ == '__main__':
    thread.start_new_thread(recvData, (2,))
    sendData(1)